/*!
\file XmlHandler.h
\brief Definition of XmlHandler class.
\ingroup Graph
*/
#ifndef XMLHANDLER_H
#define XMLHANDLER_H

#include<QtXml>
#include<stdio.h>

#include"Graph.h"

/*!
\class XmlHandler
\brief Implementation of QXmlDefaultHandler to parse a Graph for us.
\ingroup Graph
*/

class XmlHandler : public QXmlDefaultHandler{
private:
	bool startDocument();
	//!< Called at start of Xml document.
	bool endDocument();
	//!< Called at end of Xml document.
	
	bool startElement( const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts );
	//!< Called on start reading element.
	bool endElement( const QString &namespaceURI, const QString &localName, const QString &qName );
	//!< Called on end reading element.
	
	bool startEntity( const QString &name );
	//!< Called on start reading entity
	bool endEntity( const QString &name );
	//!< Called on end reading entity
	
	bool comment( const QString &ch );
	//!< Called on read comment
	
	bool startDTD( const QString &name, const QString &publicID, const QString &sysID );
	//!< Called on start reading DTD
	bool endDTD();
	//!< Called on end reading DTD
	
	QString error()const{ return m_error; }
	//!< Returns error encountered when reading, or empty string if none.
	
	Graph *graph()const{ return m_graph; }
	//!< Returns the graph if properly read.
	
	void deleteGraph(){ if(m_graph!=0)delete m_graph;m_graph=0; }
	//!< Deleted the graph if any.
public:
	static Graph *fromFile( const QString &fileName, QString &err );
	//!< To be called to read a graph from file.
private:
	XmlHandler();
	~XmlHandler();

	Graph *m_graph;
	QString m_error;
	bool m_constructionInProgress;
	
	bool createGraph( const QXmlAttributes &atts );
	bool createNode( const QXmlAttributes &atts );
	bool createEdge( const QXmlAttributes &atts );
	bool parseInt( const QXmlAttributes &atts, int i,  int &ret );
};

#endif

